package com.yuexun.beilunpatient.ui.visit.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.ui.visit.adapter.VisitListAdapter;
import com.yuexun.beilunpatient.ui.visit.ui.Act_Visit_Detail;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/4/13 0013.
 */

public class Frag_Visit_Draftbox extends KJFragment implements ICallBackListener
{
    @Bind(R.id.content_list)
    PullToRefreshList contentList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private ListView mList;
    VisitListAdapter adapter;
    List<String> strings = new ArrayList<>();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        listViewPreference();
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = contentList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        contentList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) contentList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");

        strings.add("");
        strings.add("");
        adapter = new VisitListAdapter(mList, strings, R.layout.item_visit);
        mList.setAdapter(adapter);

        emptyLayout.dismiss();

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Bundle bundle2 = new Bundle();
                Intent intent = new Intent(getActivity(), Act_Visit_Detail.class);
                bundle2.putInt("type", 2);
                intent.putExtras(bundle2);
                startActivity(intent);
            }
        });
        adapter.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                strings.add("");
                adapter.refresh(strings);
            }
        });

        contentList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                contentList.setHasMoreData(false);
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            }
        });
    }

    @Override
    public void callback(Bundle bundle) {

    }
}
